{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from datasets import load_dataset\n",
    "from deepscaler.rewards.math_utils import extract_answer\n",
    "\n",
    "ds = load_dataset(\"hendrycks/competition_math\")\n",
    "\n",
    "print(ds)\n",
    "\n",
    "def extract_difficulty(level_str):\n",
    "    # Extract the number from the string and convert it to a float\n",
    "    try:\n",
    "        difficulty = float(level_str.split()[-1])\n",
    "    except (ValueError, IndexError):\n",
    "        print(f\"Error extracting difficulty from {level_str}\")\n",
    "        difficulty = 0.0\n",
    "    return difficulty\n",
    "\n",
    "\n",
    "dataset = []\n",
    "for entry in ds['train']:\n",
    "    answer = extract_answer(entry['solution'])\n",
    "    new_entry = {\n",
    "        \"problem\": entry['problem'],\n",
    "        \"answer\": answer,\n",
    "        \"difficulty\": extract_difficulty(entry[\"level\"]),\n",
    "        \"type\": entry[\"type\"]\n",
    "    }\n",
    "    dataset.append(new_entry)\n",
    "\n",
    "with open(\"../train/math.json\", \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_dataset = load_dataset(\"HuggingFaceH4/MATH-500\", trust_remote_code=True)['test']\n",
    "print(test_dataset)\n",
    "dataset = []\n",
    "for entry in test_dataset:\n",
    "    answer = extract_answer(entry['solution'])\n",
    "    new_entry = {\n",
    "        \"problem\": entry['problem'],\n",
    "        \"answer\": answer,\n",
    "        \"difficulty\": float(entry[\"level\"]),\n",
    "        \"unique_id\": entry[\"unique_id\"]\n",
    "    }\n",
    "    dataset.append(new_entry)\n",
    "\n",
    "with open(\"../test/math.json\", \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tsj",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
